﻿using System.Collections.ObjectModel;
using System.IO;
using System.Xml;

namespace GlobalizationTools.Model
{
    public class Project
    {
        Solution _solution = null;
        string _projectName;
        string _filePath;

        XmlDocument xmlDocument = new XmlDocument();
        XmlNamespaceManager xmlNamespaceManager;
        bool _isLoaded = false;
        Collection<Resource> _resources = new Collection<Resource>();

        public Project(string path, string name, Solution solution)
        {
            _solution = solution;
            _filePath = path;
            _projectName = name;
        }

        public Project(string path)
        {
            _filePath = path;
            _projectName = Path.GetFileNameWithoutExtension(path);
        }

        public void LoadProject()
        {
            xmlDocument.Load(_filePath);

            xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
            xmlNamespaceManager.AddNamespace("msbuild", "http://schemas.microsoft.com/developer/msbuild/2003");

            XmlNodeList nodes = xmlDocument.SelectNodes("//msbuild:EmbeddedResource", xmlNamespaceManager);
            foreach (XmlNode node in nodes)
            {
                string _include = node.Attributes["Include"].Value;
                if (Path.GetExtension(_include) == ".resx")
                {
                    Resource resource = new Resource(node, this);
                    _resources.Add(resource);
                }
            }
            _isLoaded = true;
        }

        public Solution Solution
        {
            get { return _solution; }
        }

        public string ProjectName
        {
            get { return _projectName; }
        }

        public bool IsLoaded
        {
            get { return _isLoaded; }
        }

        public string FilePath
        {
            get { return _filePath; }
        }

        public Collection<Resource> Resources
        {
            get { return _resources; }
        }


    }
}
